package cn.itsource.product.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageBean;
import cn.itsource.product.domain.Product;
import cn.itsource.product.domain.ProductDetail;
import cn.itsource.product.query.ProductQuery;
import cn.itsource.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 分页查询和高级查询
     * @param productQuery
     * @return
     */
    @PostMapping
    public PageBean<Product> getProducts(@RequestBody ProductQuery productQuery){

        return productService.queryPage(productQuery);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id){
        try {
            productService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 根据productid查询ProductDetail
     * @return
     */
    @PostMapping("/getProductDetail")
    public List<ProductDetail> getProductDetail(@RequestBody Product product){

        return productService.selectProductDetail(product.getId());
    }

    /**
     * 添加修改
     * @return
     */
    @PutMapping
    public AjaxResult saveAndUp(@RequestBody Product product){
        try {
            System.out.println(product);
            if(product.getId()==null){
                productService.save(product);
            }else {
                productService.update(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 商品上架
     * @return
     */
    @PostMapping("/onsale")
    public  AjaxResult onsale(@RequestBody List<Product> products){
        try {
            productService.onsale(products);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 商品下架
     * @param products
     * @return
     */
    @PostMapping("/offsale")
    public  AjaxResult offsale(@RequestBody List<Product> products){
        products.forEach(System.out::println);
        try {
            productService.offsale(products);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param products
     * @return
     */
    @PostMapping("/batchRemove")
    public  AjaxResult batchRemove(@RequestBody List<Product> products){
        products.forEach(System.out::println);
        try {
            productService.batchRemove(products);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }


    //-----------------------门户网站----------------------------

    /**
     * 门户网站分页查询和高级查询
     * @param productQuery
     * @return
     */
    @PostMapping("/queryHomePage")
    @ResponseBody
    public PageBean<Product> queryHomePage(@RequestBody ProductQuery productQuery){

        return productService.queryPage(productQuery);
    }

    /**
     * 获取product和详细内容
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Product getProductById(@PathVariable Long id){
        //System.out.println(productService.getProductById(id));
        return productService.getProductById(id);

    }



}
